<?php

require_once "MainController.php";

/**
 * Contrôleur de mediathèque
 *
 * @package Controller
 * @author  nroche <nicolas.roche42@gmail.com>
 *
 */
class LibraryController extends MainController
{
    /**
     * Action par défaut
     *
     * @return null
     */
    public function indexAction()
    {
        // .js library
        $this->view->headScript()->appendFile($this->view->baseUrl() . "/js/rating.js");
        $this->view->headScript()->appendFile($this->view->baseUrl() . "/js/library.js");

        $idUser = $this->_sessionManager->getUserId();

        $userMapper = Adapter::getUserMapper();

        //         $movieMapper = new App_Model_Map_Movie();
        //         $movieList = $movieMapper->getMoviesByUserID($idUser);
        //         $this->view->nbMovies = count($movieList);
        //         $this->view->movieList = $movieList;

        $friends = $userMapper->getFriendsByUserID($idUser);
        $this->view->friends = $friends;

    }


    /**
     *
     * GET : /getmovies/u/{idUser}
     */
    public function getmoviesAction()
    {
        try {

            $idConnectedUser = $this->_sessionManager->getUserId();

            $params = $this->_request->getParams();

            if (!isset($params['u'])) {
                $this->_response->clearBody();
                $this->_response->clearHeaders();
                $this->_response->setHttpResponseCode(406);
                return;
            }

            $idUser = $params['u'];
            $idUser = $this->view->escape($idUser);

            // TODO tester si idUser est un int

            $movieMapper = new App_Model_Map_Movie();

            if ($idUser == $idConnectedUser)
                $movieList = $movieMapper->getMoviesByUserID($idUser);
            else {
                // TODO: verifier qu'ils sont amis
                $movieList = $movieMapper->getFriendMovies($idConnectedUser, $idUser);
            }

            //             var_dump($movieList);die;

            // on envoie les traductions des colonnes du tableau
            $translate = Zend_Registry::get('Zend_Translate');
            $translations = array(
                    'name_fr' => $translate->translate("library_movies_name_fr"),
                    'name_orig' => $translate->translate("library_movies_name_orig"),
                    'my_rating' => $translate->translate("library_movies_my_rating"),
                    'his_rating' => $translate->translate("library_movies_his_rating"),
                    'avg_rating' => $translate->translate("library_movies_avg_rating"),
                    'release' => $translate->translate("library_movies_release"),
                    'format' => $translate->translate("library_movies_format"),
                    'quality' => $translate->translate("library_movies_quality"),
                    'languages' => $translate->translate("library_movies_languages"),
                    'subtitles' => $translate->translate("library_movies_subtitles"),
                    'watched' => $translate->translate("library_movies_watched"),
                    'remove' => $translate->translate("library_movies_remove"),
            );

            $return = array(
                    'success' => true,
                    'idUser' => $idConnectedUser,
                    'movies' => $movieList,
                    'count' => count($movieList),
                    'translations' => $translations,
            );

            $this->_helper->json($return);
        }
        catch (Exception $e) {
            var_dump($e);die;
            $return = array('success' => false);
            $this->_helper->json($return);
        }
    }

    /**
     * http://localhost/mediaglobe/library/savemoviedata/movie/1/rating/4
     */
    public function savemoviedataAction()
    {
        try {
            $returnSuccess = false;
            $returnmessage = '';

            $params = $this->_request->getParams();
            if (!isset($params['movie'])) {
                $returnmessage = "Mauvais parametres";
            }
            else {
                $idUser = $this->_sessionManager->getUserId();
                $idMovie = $params['movie'];
                $movieMapper = new App_Model_Map_Movie();

                if (isset($params['rating'])) {
                    $newRating = $params['rating'];
                    $data = array('rating' => $newRating);
                    $movieMapper->updateLinkUserMovie($idUser, $idMovie, $data);
                    $returnSuccess = true;
                }
                // ajouter les autres ici...
            }
        }
        catch (Exception $e) {
//             var_dump($e);die;
//             $return = array('success' => false);
//             $this->_helper->json($return);
            $returnmessage = "Erreur: exception";
        }

        $return = array(
                'success' => $returnSuccess,
                'message' => $returnmessage,
        );
        $this->_helper->json($return);
    }

}

